package org.dreamwork.tools.jfx;

import javafx.application.Platform;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;
import javafx.stage.StageStyle;
import org.dreamwork.tools.LocalUtil;

/**
 * Created by seth.yang on 2018/12/10
 */
public class FXUpdateShell {
    private static Stage stage;

    public static void start () throws Exception {
        if (stage == null) {
//            Application.launch (FXUpdateShell.class);
            FXUpdateShell shell = new FXUpdateShell ();
            Platform.runLater (()-> {
                try {
                    shell.start (new Stage ());
                } catch (Exception e) {
                    e.printStackTrace ();
                }
            });
        } else {
            if (Platform.isFxApplicationThread ()) {
                stage.show ();
            } else {
                Platform.runLater (()->stage.show ());
            }
        }
    }

    private void start (Stage primaryStage) throws Exception {
        stage = primaryStage;
        FXMLLoader loader = new FXMLLoader (getClass ().getClassLoader ().getResource ("check-update.fxml"));
        loader.setResources (LocalUtil.ui);

        Parent root = loader.load ();
        UpdateChecker checker = loader.getController ();
        Scene scene = new Scene (root);
        primaryStage.setScene (scene);
        checker.setStage (primaryStage);

//        primaryStage.initModality (Modality.APPLICATION_MODAL);
        primaryStage.initStyle (StageStyle.UNDECORATED);
        primaryStage.show ();
        LocalUtil.service.execute (checker::check);
    }
}